<?php
namespace Admin\Model;

use Think\Model;

class CertificateModel extends Model
{
    protected $connection = 'XYC';
    protected $trueTableName = 'certificate';
    protected $_validate = array(
        array('numbering', 'require', '订单号不能为空'),
        array('loan_time', 'require', '借款时间不能为空'),
        array('repayment_time', 'require', '还款时间不能为空'),
        array('money', 'check_money', '借款金额不得小于等于0','0','callback'),
        // array('interest_rate', 'require', '年利率不得为空'),
        // array('interest', 'check_money', '利息不得小于等于0','0','callback'),
        array('lender', 'require', '出借人不得为空'),
        array('lender_id', 'require', '出借人不得为空'),
        array('lender_signature', 'require', '出借人暂未设置签名'),
        array('card_id', 'require', '借款人身份证号不能为空'),
        array('borrower', 'require', '借款人姓名不能为空'),
        array('borrower_id', 'require', '借款人不能为空'),
//        array('borrower_signature', 'require', '借款人暂未设置签名'),
        array('mobile', 'require', '借款人手机号不能为空'),
        array('shop_identifier', 'require', '商户号不能为空'),
        array('borrower_id', 'check_user', '该借款人已被限制借款','0','callback'),
    );

    /**
     * @param int $param
     * @return bool
     */
    public function check_money($param){
        if($param<=0){
            return false;
        }else{
            return true;
        }
    }

    public function check_user($param){
        $msg=M('user_login','','XYC')->WHERE(['user_id'=>$param])->FIND();
        if($msg['approve_state']!='0'){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 修改凭证状态
     * @param string $orderno 凭证订单
     * @param string $shop_identifier 商户号
     * @param int $status 状态
     * @return string|boolean
     */
    public function set_status($orderno,$status,$real_repayment_money='',$real_repayment_time=''){
        $res=$this->WHERE(['numbering'=>$orderno,'shop_identifier'=>$_SESSION['shop_identifier']])->FIND();
        if(empty($res)){
            return '未发现该订单';
        }
        if($res['status']==$status){
            return '状态异常，修改失败';
        }
        switch($status){
            case '5'://已坏账
                if($res['status']=='6'){
                    return '该订单已撕毁，无法标记坏账';
                }
                break;
            case '6'://撕毁
                if(empty($real_repayment_money)){
                    $real_repayment_money=$this->WHERE(['numbering'=>$orderno,'shop_identifier'=>$_SESSION['shop_identifier']])->getField('money');
                }
                if(empty($real_repayment_time)){
                    $real_repayment_time=time();
                }
                if($real_repayment_money<=0)return '实际到账金额有误';
                $res=$this->WHERE(['numbering'=>$orderno,'shop_identifier'=>$_SESSION['shop_identifier']])->SAVE(['tear_time'=>time(),'real_repayment_money'=>$real_repayment_money,'real_repayment_time'=>$real_repayment_time]);
                if($res===false) return '系统异常';
                break;
            case '7'://拒绝
                break;
            default :
                return '系统异常';
        }
        $res=$this->WHERE(['numbering'=>$orderno,'shop_identifier'=>$_SESSION['shop_identifier']])->setField('status',$status);
        if($res!==false){
            return true;
        }else{
            return '系统异常';
        }
    }

    /**
     * 删除订单号
     * @param string $orderno 凭证订单号
     * @return boolean
     */
    public function del_certificate($orderno){
        $res=$this->WHERE(['numbering'=>$orderno,'shop_identifier'=>$_SESSION['shop_identifier']])->FIND();
        if(!empty($res)){
//            1未确认,2已确认,3已逾期,4续期,5已坏账,6已撕毁,7被拒绝9已打款
            if($res['status']!='0' && $res['status']!='6' && $res['status']!='7'){
                return '该凭证当前状态，无法删除';
            }
            $res=$this->WHERE(['numbering'=>$orderno])->setField('is_delete','-1');
            if($res!==false){
                return true;
            }else{
                return '系统错误，请联系管理员';
            }
        }else{
            return '该商户下面未发现该订单';
        }
    }

    /**
     * 打款处理
     * @param array $data
     * @return bool|string
     */
    public function check_play_money($data){
        $res=$this->WHERE(['numbering'=>$data['numbering'],'shop_identifier'=>$_SESSION['shop_identifier']])->FIND();
        if(!empty($res)){
            if($res['status']!='2'){
                return '该订单未确定或者已打款';
            }
            $res=$this->WHERE(['numbering'=>$data['numbering']])->SAVE(['status'=>'9']);
            if($res!==false){
                return true;
            }else{
                return '系统错误，请联系管理员';
            }
        }else{
            return '该商户下面未发现该订单';
        }
    }

}